package com.bill.gaty.mfidelity.activities;

import com.bill.gaty.mfidelity.R;
import com.bill.gaty.mfidelity.beans.Compatibilite;
import com.bill.gaty.mfidelity.dao.impl.CompatibiliteDaoImpl;
import com.bill.gaty.mfidelity.webservices.WebService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;

public class CompatibiliteActivity extends Activity {
private Compatibilite comp;
private ProgressBar bar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compatibilite_activity_layout);
		bar=(ProgressBar) findViewById(R.id.compatibilite_progressebar_layout);
		bar.setMax(100);
		 
		final WebService service=new WebService();
		final CompatibiliteDaoImpl compd=new CompatibiliteDaoImpl(getApplicationContext());
		
		   comp=(Compatibilite) getIntent().getExtras().get("Compatibilite");
		   if(null==comp) finishKo();
		   Thread t=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				long start=System.currentTimeMillis();
				compd.open();
				Compatibilite comptemp = compd.findByLuiEtElle(comp.getLui(), comp.getElle());
				compd.close();
				if(null==comptemp){
				 comp=service.searchCompatibilite(comp.getLui(), comp.getElle());
				}else
					comp=comptemp;
				 if(null==comp)comp=new Compatibilite();
				  Log.e("CompatibiliteActivity.service.searchCompatibilite.com",comp.toString());
				  
				  Log.e("CompatibiliteActivity.service.searchCompatibilite.com.duration",(System.currentTimeMillis()-start)+"");
				  finish();
				  
			}
		});
		   t.start();
		 
	    /*
	     * if(comp.isOk()){
	    	resultat.setText(comp.getResult());
	    }else{
	    	resultat.setText(comp.getSearchCode()+"");
	    }
	     */
	}

	private void finishKo() {
		// TODO Auto-generated method stub
		Intent retour=new Intent();
		Bundle data=new Bundle();
		data.putSerializable("Compatibilite", new Compatibilite());
		retour.putExtras(data);
		
		setResult(RESULT_CANCELED, retour);
		super.finish();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		Intent retour=new Intent();
		Bundle data=new Bundle();
		data.putSerializable("Compatibilite", comp);
		retour.putExtras(data);
		 //207100
		setResult(RESULT_OK, retour);
		super.finish();
	}

}
